package com.jwgf.smarowindingmachine_unity.ui;
import java.text.DecimalFormat;

import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

public class CommonVar {
	public static int lifter_command_start;
	public static int lifter_command_stop;
	public static int lifterflag = 0;	//判断是否落筒，1：落筒中皮带停  0：皮带动
	/** Unit transfer factor, from metre to yard */
	public static final float METRE_TO_YARD_FACTOR = 1f / 0.9144f;
	/** Unit transfer factor, from kg to lb */
	public static final float KG_TO_LB_FACTOR = 1f / 0.4536f;
	/** Unit transfer factor, from Ne to Nm */
	public static final float NE_TO_NM_FACTOR = 0.583f;
	/** Unit transfer factor, Constant of Ne multiplied by tex */
	public static final float NE_MULTI_TEX_CONST = 583.1f;
	
	public static final float LENGTH_TO_PULSE_FACTOR = 0.0099323565f;
	
	private static MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();	
	
	/**
	 * Transfer length unit from user selected to metre.
	 */
	public static float getMetreFromLength(float mLength) {
		float value = 0;
		String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT,"0");

		if (unit.equals("0")) {
			value = mLength;
		}else {
			value = mLength / METRE_TO_YARD_FACTOR;
		}

		return value;
	}
	
	/**
	 * Transfer length unit from metre to user selected.
	 */
	public static float getLengthFromMetre(float mMetre) {
		float value = 0;
		String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT,"0");

		if (unit.equals("0")) {
			value = mMetre;
		}else {
			value = mMetre * METRE_TO_YARD_FACTOR;
		}

		return value;
	}
	
	/**
	 * Transfer weight unit from gram to user selected.
	 */
	public static float getWeightFromGram(float mGram) {
		float value = 0;
		String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_WEIGHT_UNIT,"0");

		if (unit.equals("0")) {
			value = mGram / 1000;
		}else {
			value = mGram / (KG_TO_LB_FACTOR * 1000);
		}

		return value;
	}
	
	/**
	 * Transfer count unit from Ne to user selected.
	 */
	public static float getCountFromNe(float mNe) {
		float value = 0;
		String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT,"0");
		
		if (unit.equals("0")) {
			value = mNe;
		}else if (unit.equals("1")) {
			value = mNe / NE_TO_NM_FACTOR;
		}else {
			value = NE_MULTI_TEX_CONST / mNe;
		}
		return value;
	}
	
	public static float getNeFromCount(float nCount) {
		float value = 0;
		String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT,"0");//0:英支   1：公支   2：号
		
		if (unit.equals("0")) {
			value = nCount;
		}else if (unit.equals("1")) {
			value = nCount * NE_TO_NM_FACTOR;
		}else {
			value = NE_MULTI_TEX_CONST / nCount;
		}
		return value;
	}
	
	/**
	 * Transfer weight unit from user selected to gram.
	 */
	public static float getGramFromWeight(float mWeight) {
		float value = 0;
		int weight_unit = 0;

		weight_unit = Integer.parseInt(sp.getValue(MachineParameterSharedPreference.SYSTEM_WEIGHT_UNIT,"0"));

		switch (weight_unit) {
		case 0:
			value = mWeight * 1000;
			break;
		case 1:
			value = mWeight / KG_TO_LB_FACTOR * 1000;
			break;
		default:
			break;
		}

		return value;
	}
	
	public static String getWeightFromLength(String mLength,int group) {
		String value = "";
		DecimalFormat df = new DecimalFormat("0.000");
		int length_value = Integer.parseInt(mLength);
		float weight_value = getWeight(length_value,group);
		value = df.format(weight_value);

		return value;
	}
	
	public static float getWeight(float mLength,int group) {//参数单位为米
		float weight_value = 0;
		float weight_temp;
        float prod_coef = Float.parseFloat(sp.getValue(group, MachineParameterSharedPreference.SPINDLE_PCF));
		float yarn_count = Float.parseFloat(sp.getValue(group, 0x28, 1));
		int ends_num = Integer.parseInt(sp.getValue(group, MachineParameterSharedPreference.SPINDLE_ENDS_NUM));

		int mGram = Math.round(mLength * prod_coef / (1.71f * yarn_count * ends_num));

		weight_temp = getWeightFromGram(mGram);// g
		weight_value = Math.round(weight_temp * 1000) / 1000f;

		return weight_value;
	}

}
